package com.elsa.DataStatistics.statistics;

import com.elsa.DataStatistics.mapper.*;
import com.elsa.DataStatistics.pojo.Menu;
import com.elsa.DataStatistics.pojo.MenuOrder;
import com.elsa.DataStatistics.pojo.MonthPersonal;
import com.elsa.DataStatistics.pojo.YearPersonal;
import com.elsa.DataStatistics.utils.CalculateUtil;
import com.elsa.DataStatistics.utils.TimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class YearPersonalStatistics {

    @Autowired
    private TimeUtil timeUtil;

    @Autowired
    private DayMapper dayMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private DayMpMapper dayMpMapper;

    @Autowired
    private CalculateUtil calculateUtil;

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private YearPersonalMapper yearPersonalMapper;

    @Autowired
    private MenuOrderMapper menuOrderMapper;

    public void yearPersonalStatistics(String oId) {

        int yId = timeUtil.getYearTime();

        List<MenuOrder> menuOrderList = menuOrderMapper.selectMenuOrderMapperFromOrderDataStatistics(oId);

        for (int i = 0; i < menuOrderList.size(); i++) {
            YearPersonal yearPersonal = yearPersonalMapper.selectYearPersonalByDIdAndCId(yId, menuOrderList.get(i).getCId());
            if(yearPersonal == null) {
                yearPersonal = new YearPersonal();
                yearPersonal.setYpsId(yearPersonalMapper.selectCount(null) + 1);
                yearPersonal.setYpId(yId);
                yearPersonal.setCId(menuOrderList.get(i).getCId());
                Menu menu = menuMapper.selectById(menuOrderList.get(i).getMId());
                yearPersonal.setYpExpend(menu.getMPrice() * menuOrderList.get(i).getMoNum());
                yearPersonal.setYpCalorie(menu.getMCalorie() * menuOrderList.get(i).getMoNum());
                yearPersonalMapper.insert(yearPersonal);
            } else {
                Menu menu = menuMapper.selectById(menuOrderList.get(i).getMId());
                yearPersonal.setYpExpend(yearPersonal.getYpExpend() + menu.getMPrice() * menuOrderList.get(i).getMoNum());
                yearPersonal.setYpCalorie(yearPersonal.getYpCalorie() + menu.getMCalorie() * menuOrderList.get(i).getMoNum());
                yearPersonalMapper.updateById(yearPersonal);
            }
        }
    }

}
